package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.HomePageDAO;
import com.diankangwo.domain.HomePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 首页banner控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "首页banner")
public class HomePageController {

    @Autowired
    private HomePageDAO homePageDao;

    /**
     * 获取横幅列表
     *
     * @return
     */
    @ApiOperation(value=" 获取横幅列表", notes="")
    @ResponseBody
    @RequestMapping(value="/banner/list", method=RequestMethod.GET)
    public String getList() {
        List<HomePage> homePageList=homePageDao.findAll();
        String code="2";
        String msg="处理异常！";
        if (homePageList != null) {
            code="1";
            msg="查询成功！";
        } else {
            msg="未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, homePageList);
    }

    /**
     * 增加banner数据
     *
     * @param picSrc      图片地址
     * @param params      uri附带参数
     * @param bgColor     图片背景色值
     * @param showPageUri 要跳转的uri地址
     * @return
     */
    @ApiOperation(value=" 增加banner数据", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name="picSrc", value="图片地址", required=true, dataType="String"),
            @ApiImplicitParam(name="params", value="uri附带参数", required=true, dataType="String"),
            @ApiImplicitParam(name="bgColor", value="图片背景色值", required=true, dataType="String"),
            @ApiImplicitParam(name="showPageUri", value="要跳转的uri地址", required=true, dataType="String")
    })
    @ResponseBody
    @RequestMapping(value="/banner/insert", method=RequestMethod.POST)
    public String insert(String picSrc, String showPageUri, String params, String bgColor) {
        String code="2";
        String msg="处理异常！";
        if(!picSrc.equals("")||!showPageUri.equals("")||!params.equals("")||!bgColor.equals("")||picSrc != null||showPageUri != null || params != null || bgColor != null){
            HomePage hp = new HomePage();
            hp.setBgColor(bgColor);
            hp.setParams(params);
            hp.setPicSrc(picSrc);
            hp.setShowPageUri(showPageUri);
            if(homePageDao.saveOrUpdate(hp) != null){
                code="1";
                msg="添加成功！";
            }
        }
        return resultMsg.mapToJson(code, msg, null);
    }
}
